/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiof.moraa.server.service;

import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.cpr.DefaultBroadcaster;
import org.atmosphere.cpr.DefaultBroadcasterFactory;

/**
 * @author Bergy
 */
public class SendMessageService {

    /*MessageChannel outboundChannel;
    XMPPConnection connection;
    MultiUserChat groupChat;
    protected final Log logger = LogFactory.getLog(getClass());

    public void setOutboundChannel(MessageChannel outboundChannel) {
        this.outboundChannel = outboundChannel;
    }

    public void setConnection(XMPPConnection connection) {
        this.connection = connection;
    }

    public Message sendMessageToBuddy(String buddy, String message) {
        logger.info("Send message to " + buddy + " message");
        Message<String> xmppOutboundMsg =
                MessageBuilder.withPayload(message).setHeader(XmppHeaders.TO, getXMPPUserName(buddy)).build();
        outboundChannel.send(xmppOutboundMsg);
        logger.info("Send message finish");
        return xmppOutboundMsg;
    }

    private void initializeGroupChat() {
        try {
            if (!connection.isConnected()) {
                connection.connect();
            }
            if (groupChat == null) {
                groupChat = new MultiUserChat(connection, "notification@conference.klimatur.hiof.no");
            }
            if (!groupChat.isJoined()) {
                groupChat.join("norseman_server");
            }
        } catch (XMPPException ex) {
            Logger.getLogger(SendMessageService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
*/
    public void sendMessageToBrowser(String message) {
//        initializeGroupChat();
//        try {
//            groupChat.sendMessage(message);
//        } catch (XMPPException ex) {
//            Logger.getLogger(SendMessageService.class.getName()).log(Level.SEVERE, null, ex);
//        }                
        Broadcaster singleBroadcaster = DefaultBroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, "streaming");
        if (singleBroadcaster != null) {
            singleBroadcaster.broadcast(message);
        }
    }
/*
    private String getXMPPUserName(String username) {
        return username + "@klimatur.hiof.no";
    }*/
}
